from datetime import datetime
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from base64 import b64encode, b64decode
from urllib.parse import quote_plus
from urllib.parse import urlparse, parse_qs
from urllib.request import urlopen
from base64 import decodebytes, encodebytes
import requests

import json

import os
from drf_movie_rimi import settings

<<<<<<< HEAD
# 需要安装一个 pycrypto

if __name__ == "__main__":


    import sys, os
    from django.core.wsgi import get_wsgi_application

    base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.append(base_dir)
    os.environ['DJANGO_SETTINGS_MODULE'] = 'drf_movie_rimi.settings'
    application = get_wsgi_application()
    from rest_framework.response import Response

    class AliPay(object):
        """
        支付宝支付接口
        """

        def __init__(self, debug=True):
            #2088102176222065
            #2018080660974232
            self.appid = '2016091800541924'
            self.app_notify_url = 'http://176.122.175.246:8000/mytest/test6'
            self.app_private_key_path = os.path.join(settings.BASE_DIR,'keys','rsa_private_key.pem')
            self.app_private_key = None
            self.return_url = 'http://127.0.0.1:8000/mytest/test6/'
            with open(self.app_private_key_path) as fp:
                self.app_private_key = RSA.importKey(fp.read())

            alipay_public_key_path = os.path.join(settings.BASE_DIR, 'keys', 'ali_pub')

            self.alipay_public_key_path = alipay_public_key_path
            with open(self.alipay_public_key_path) as fp:
                self.alipay_public_key = RSA.import_key(fp.read())

            if debug is True:
                self.__gateway = "https://openapi.alipaydev.com/gateway.do"
            else:
                self.__gateway = "https://openapi.alipay.com/gateway.do"

        def direct_pay(self, subject='test', out_trade_no='taqasdfr1', total_amount=10.0, return_url=None, **kwargs):
            biz_content = {
                "subject": subject,
                "out_trade_no": out_trade_no,
                "total_amount": total_amount,
                "product_code": "FAST_INSTANT_TRADE_PAY",
                # "qr_pay_mode":4
            }

            biz_content.update(kwargs)
            data = self.build_body("alipay.trade.page.pay", biz_content, self.return_url)
            return self.sign_data(data)

        def build_body(self, method, biz_content, return_url=None):
            data = {
                "app_id": self.appid,
                "method": method,
                "charset": "utf-8",
                "sign_type": "RSA2",
                "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                "version": "1.0",
                "biz_content": biz_content
            }

            if return_url is not None:
                data["notify_url"] = self.app_notify_url
                data["return_url"] = self.return_url

            return data

        def sign_data(self, data):
            data.pop("sign", None)
            # 排序后的字符串
            unsigned_items = self.ordered_data(data)
            unsigned_string = "&".join("{0}={1}".format(k, v) for k, v in unsigned_items)
            sign = self.sign(unsigned_string.encode("utf-8"))
            #sign = '123'
            # ordered_items = self.ordered_data(data)
            quoted_string = "&".join("{0}={1}".format(k, quote_plus(v)) for k, v in unsigned_items)

            # 获得最终的订单信息字符串
            signed_string = quoted_string + "&sign=" + quote_plus(sign)
            return signed_string

        def ordered_data(self, data):
            complex_keys = []
            for key, value in data.items():
                if isinstance(value, dict):
                    complex_keys.append(key)

            # 将字典类型的数据dump出来
            for key in complex_keys:
                data[key] = json.dumps(data[key], separators=(',', ':'))

            return sorted([(k, v) for k, v in data.items()])

        def sign(self, unsigned_string):
            # 开始计算签名
            key = self.app_private_key
            signer = PKCS1_v1_5.new(key)
            signature = signer.sign(SHA256.new(unsigned_string))
            # base64 编码，转换为unicode表示并移除回车
            sign = encodebytes(signature).decode("utf8").replace("\n", "")
            return sign

        def _verify(self, raw_content, signature):
            # 开始计算签名
            key = self.alipay_public_key
            signer = PKCS1_v1_5.new(key)
            digest = SHA256.new()
            digest.update(raw_content.encode("utf8"))
            if signer.verify(digest, decodebytes(signature.encode("utf8"))):
                return True
            return False

        def verify(self, data, signature):
            if "sign_type" in data:
                sign_type = data.pop("sign_type")
            # 排序后的字符串
            unsigned_items = self.ordered_data(data)
            message = "&".join(u"{}={}".format(k, v) for k, v in unsigned_items)
            return self._verify(message, signature)

    alipay = AliPay(
            debug=True,  # 默认False,
        )

    url = alipay.direct_pay(
            subject="luffycity",
            out_trade_no="21101701zsdfaf32012311qwe41421349022223486",
            total_amount=100.00,
            #return_url="http://176.122.175.246:8000/mytest/test6/"
            return_url="http://127.0.0.1:8000/mytest/test6/"
        )

    res = url
    re_url = "https://openapi.alipaydev.com/gateway.do?{data}".format(data=url)
    #x =requests.get(re_url)

    print(Response(re_url))
 # a =
=======

class AliPay(object):
    """
    支付宝支付接口
    """

    def __init__(self, debug=True):
        #2088102176222065
        #2018080660974232
        self.appid = '2016091800541924'
        self.app_notify_url = 'http://127.0.0.1:8000/docs/'
        self.app_private_key_path = os.path.join(settings.BASE_DIR,'keys','rsa_private_key.pem')
        self.app_private_key = None
        self.return_url = 'http://176.122.175.246:8000/mytest/test6/'
        with open(self.app_private_key_path) as fp:
            self.app_private_key = RSA.importKey(fp.read())

        alipay_public_key_path = os.path.join(settings.BASE_DIR, 'keys', 'rsa_public_key.pem')

        self.alipay_public_key_path = alipay_public_key_path
        with open(self.alipay_public_key_path) as fp:
            self.alipay_public_key = RSA.import_key(fp.read())

        if debug is True:
            self.__gateway = "https://openapi.alipaydev.com/gateway.do"
        else:
            self.__gateway = "https://openapi.alipay.com/gateway.do"

    def direct_pay(self, subject='test', out_trade_no='taqasdfr1', total_amount=10.0, return_url=None, **kwargs):
        biz_content = {
            "subject": subject,
            "out_trade_no": out_trade_no,
            "total_amount": total_amount,
            "product_code": "FAST_INSTANT_TRADE_PAY",
            # "qr_pay_mode":4
        }

        biz_content.update(kwargs)
        data = self.build_body("alipay.trade.page.pay", biz_content, self.return_url)
        return self.sign_data(data)

    def build_body(self, method, biz_content, return_url=None):
        data = {
            "app_id": self.appid,
            "method": method,
            "charset": "utf-8",
            "sign_type": "RSA2",
            "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "version": "1.0",
            "biz_content": biz_content
        }

        if return_url is not None:
            data["notify_url"] = self.app_notify_url
            data["return_url"] = self.return_url

        return data

    def sign_data(self, data):
        data.pop("sign", None)
        # 排序后的字符串
        unsigned_items = self.ordered_data(data)
        unsigned_string = "&".join("{0}={1}".format(k, v) for k, v in unsigned_items)
        sign = self.sign(unsigned_string.encode("utf-8"))
        #sign = '123'
        # ordered_items = self.ordered_data(data)
        quoted_string = "&".join("{0}={1}".format(k, quote_plus(v)) for k, v in unsigned_items)

        # 获得最终的订单信息字符串
        signed_string = quoted_string + "&sign=" + quote_plus(sign)
        return signed_string

    def ordered_data(self, data):
        complex_keys = []
        for key, value in data.items():
            if isinstance(value, dict):
                complex_keys.append(key)

        # 将字典类型的数据dump出来
        for key in complex_keys:
            data[key] = json.dumps(data[key], separators=(',', ':'))

        return sorted([(k, v) for k, v in data.items()])

    def sign(self, unsigned_string):
        # 开始计算签名
        key = self.app_private_key
        signer = PKCS1_v1_5.new(key)
        signature = signer.sign(SHA256.new(unsigned_string))
        # base64 编码，转换为unicode表示并移除回车
        sign = encodebytes(signature).decode("utf8").replace("\n", "")
        return sign

    def _verify(self, raw_content, signature):
        # 开始计算签名
        key = self.alipay_public_key
        signer = PKCS1_v1_5.new(key)
        digest = SHA256.new()
        digest.update(raw_content.encode("utf8"))
        if signer.verify(digest, decodebytes(signature.encode("utf8"))):
            return True
        return False

    def verify(self, data, signature):
        if "sign_type" in data:
            sign_type = data.pop("sign_type")
        # 排序后的字符串
        unsigned_items = self.ordered_data(data)
        message = "&".join(u"{}={}".format(k, v) for k, v in unsigned_items)
        return self._verify(message, signature)

alipay = AliPay(
        debug=True,  # 默认False,
    )

url = alipay.direct_pay(
        subject="luffycity",
        out_trade_no="201702029022314286",
        total_amount=100.00,
        return_url="http://176.122.175.246:8000/mytest/test6/"
    )

res = url
re_url = "https://openapi.alipaydev.com/gateway.do?{data}".format(data=url)
x =requests.get(re_url)


a = 1
>>>>>>> cf42d09fc87caf6b213a2b475c348fbeb44c0fdb
